package com.sixmac.controller;

import com.google.common.base.Strings;
import com.sixmac.common.base.BaseController;
import com.sixmac.core.plugins.dao.Blade;
import com.sixmac.core.toolbox.ajax.AjaxResult;
import com.sixmac.core.toolbox.kit.JsonKit;
import com.sixmac.model.RoomHouse;
import com.sixmac.model.RoomLog;
import com.sixmac.service.RoomLogService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

/**
 * RoomLogController
 * 2018-10-18 20:13:54
 *
 * @author Blade
 */
@Controller
@RequestMapping("/roomLog")
public class RoomLogController extends BaseController {
    private static String CODE = "roomLog";
    private static String PREFIX = "room_log";
    private static String LIST_SOURCE = "roomLog.list";
    private static String BASE_PATH = "/platform/roomLog/";

    @Autowired
    RoomLogService service;

    @RequestMapping(KEY_MAIN)
    public String index(ModelMap mm, String id) {
        mm.put("code", CODE);
        RoomHouse house = Blade.create(RoomHouse.class).findById(id);
        mm.put("c", house.getCode());
        return BASE_PATH + "roomLog.html";
    }

    @RequestMapping(KEY_MAIN + "1")
    public String index1(ModelMap mm, String id) {
        mm.put("code", CODE);
        return BASE_PATH + "roomLog.html";
    }

    @RequestMapping(KEY_ADD)
    public String add(ModelMap mm) {
        mm.put("code", CODE);
        return BASE_PATH + "roomLog_add.html";
    }

    @RequestMapping(KEY_EDIT + "/{id}")
    public String edit(@PathVariable Integer id, ModelMap mm) {
        RoomLog roomLog = service.findById(id);
        mm.put("model", JsonKit.toJson(roomLog));
        mm.put("id", id);
        mm.put("code", CODE);
        return BASE_PATH + "roomLog_edit.html";
    }

    @RequestMapping(KEY_VIEW + "/{id}")
    public String view(@PathVariable Integer id, ModelMap mm) {
        RoomLog roomLog = service.findById(id);
        mm.put("model", JsonKit.toJson(roomLog));
        mm.put("id", id);
        mm.put("code", CODE);
        return BASE_PATH + "roomLog_view.html";
    }

    @ResponseBody
    @RequestMapping(KEY_LIST)
    public Object list(String c) {
        if (Strings.isNullOrEmpty(c)) {
            c = "";
        }
        String sql = "select *,if(type=1,'操作级别','应用级别') as typeName from room_log where type =1 and details like '%BB%'".replace("BB", c);
        Object grid = paginate(sql);
        return grid;
    }

    @ResponseBody
    @RequestMapping(KEY_SAVE)
    public AjaxResult save() {
        RoomLog roomLog = mapping(PREFIX, RoomLog.class);
        boolean temp = service.save(roomLog);
        if (temp) {
            return success(SAVE_SUCCESS_MSG);
        } else {
            return error(SAVE_FAIL_MSG);
        }
    }

    @ResponseBody
    @RequestMapping(KEY_UPDATE)
    public AjaxResult update() {
        RoomLog roomLog = mapping(PREFIX, RoomLog.class);
        boolean temp = service.update(roomLog);
        if (temp) {
            return success(UPDATE_SUCCESS_MSG);
        } else {
            return error(UPDATE_FAIL_MSG);
        }
    }

    @ResponseBody
    @RequestMapping(KEY_REMOVE)
    public AjaxResult remove(@RequestParam String ids) {
        int cnt = service.deleteByIds(ids);
        if (cnt > 0) {
            return success(DEL_SUCCESS_MSG);
        } else {
            return error(DEL_FAIL_MSG);
        }
    }
}
